**********************************SETUP**********************************

clear all
set more off

//Which operating system?
	local mac					0	//Mac or PC file paths (1 for Mac)
			
	
// Set Current Directory and file paths for Mac users
if `mac'{	
	cd "/Users/jacobfrench/Dropbox (ASU)/InfoJacob/SCE Information Experiment" 
	global data			"Data/"
	global output		"Output/"
	global code			"Code/"
	global file_figs	"Figures/"
	global output_figs	"Output/Figures/"
}

// Set Current Directory and file paths for PC users
if !`mac'{	
	cd "C:\Users\bzafar\Dropbox\Information Acquisition\FINAL FILES\Appendix B"
	global data			"Data\"
	global output		"Output\"
	global code			"Code\"
}
	
//Open and initialize report file
{
file open table using ${output}supp_analysis.tex, write replace
local fwt "file write table"

*LaTeX Header and Title
`fwt' "\documentclass{article} \usepackage{multirow} \usepackage{graphicx} \usepackage{booktabs} \usepackage{float} \usepackage{pdflscape}" _n
`fwt' "\usepackage{graphicx, wrapfig, subcaption, setspace, booktabs} \usepackage[font=small, labelfont=bf]{caption}" _n
`fwt' "\usepackage[margin=.75in]{geometry} \newcommand{\HRule}[1]{\rule{\linewidth}{#1}}" _n
`fwt' "\begin{document}"_n
`fwt' "\newpage"_n
}

use $data/SuppData.dta, clear

*******************************Create Vars*****************************
{
xtset responseid date
** Define expected house prices
gen prior_value_1yf=qa1_1_2
label var prior_value_1yf "Expected Value 1 year in future (Prior Belief)"

gen prior_change_1yf=(qa1_1_2-206300)/206300
label var prior_change_1yf "Expected Value 1 year in future (Prior Belief)"

gen uncert_prior_change_1yf=qa2_sd/206300
label var uncert_prior_change_1yf "SD of Belief, Expected Value 1 year in future (Prior Belief)"

** Idem above, but for the posterior belief
gen post_value_1yf=qa4_1_2
label var post_value_1yf "Expected Value 1 year in future (Posterior Belief)"

gen post_change_1yf=(qa4_1_2-206300)/206300
label var post_change_1yf "Expected Change 1 year in future (Posterior Belief)"

gen uncert_post_change_1yf=qa5_sd/206300
label var uncert_post_change_1yf "SD of Belief, Expected Value 1 year in future (Posterior Belief)"

** Reward Treatment indicator
gen high_reward=qa3v==1
gen reward_size=(1+9*high_reward)
label var high_reward "Reward"

** Numericy Binary Variable
gen numeracy_bi=.
replace numeracy_bi=0 if total_numeracy<=4
replace numeracy_bi=1 if total_numeracy>4
label var numeracy_bi "Numeracy"

** Education Binary Variable
gen education_bi=(education>4) if education!=.
label var education_bi "Education"


****************Here Starts New Variables Not In Original Paper***************

** Prefer 1yr info
gen pref1=1 if qi==1
replace pref1=0 if qi==2
label var pref1 "Prefer 1yr $|$ Info"

** Indicator for not wanting more information
gen more_info=1 if qia==1 | qib==1
replace more_info=0 if qia==2 | qib==2
label var more_info "Prefer Both $|$ Info"

** Indicator for prefering expert info
gen pref_expert=1 if qifa==1
replace pref_expert=0 if pref_expert!=1
label var pref_expert "Prefer Expert"

** Beliefs about experts
rename qifb expert_accu
label var expert_accu "Belief: Expert Accu"
rename qifc expert_cred
label var expert_cred "Belief: Expert Cred"

** Beliefs about experts binary
gen expert_accu_bi=1 if expert_accu==4 | expert_accu==5
replace expert_accu_bi=0 if expert_accu!=. &  expert_accu<4
label var expert_accu_bi "Find Experts Accurate"
gen expert_cred_bi=1 if expert_cred==4 | expert_cred==5
replace expert_cred_bi=0 if expert_cred!=. &  expert_cred<4
label var expert_cred_bi "Trust Experts"


** Indicator for prefering any info
gen pref_info=1 if qi<3
replace pref_info=0 if qi==3
label var pref_info "Pref Any Info"

** Find information Exposure
rename qa3v2 block

gen info_0=1 if block==1
replace info_0=0 if block!=1
label var info_0 "Treatment: No Info"

gen info_1=1 if block==2 |block==5
replace info_1=0 if info_1!=1
label var info_1 "Treatment: 1 Piece of Info"

gen info_2=1 if block==3 | block==4
replace info_2=0 if info_2!=1
label var info_2 "Treatment: 2 Pieces of Info"

*Revision in 1year forward beliefs
gen revision_change_1yf=post_change_1yf-prior_change_1yf

}

*******************************Address Outliers*****************************
{
**drop if not info preference reported (preference for no info is fine)
drop if qi==.

**Note: This section taken from original paper

* Windzorise uncertainty outcomes
foreach var in uncert_prior_change_1yf uncert_post_change_1yf {
centile `var', c(1 99)
local min=`r(c_1)'
local max=`r(c_2)'
replace `var'=`min' if `var'<`min' & `var'!=.
replace `var'=`max'  if `var'>`max' & `var'!=.
}

** Drop outliers
drop if prior_change_1yf==.
centile prior_change_1yf, c(2.5 97.5)
local min=`r(c_1)'
local max=`r(c_2)'
drop if prior_change_1yf<`min'
drop if prior_change_1yf>`max'

** Censor posterior beliefs to avoid extreme effects of extreme outliers (i.e., typos)
replace post_change_1yf=`min' if post_change_1yf<`min' & post_change_1yf!=.
replace post_change_1yf=`max' if post_change_1yf>`max' & post_change_1yf!=.

* Windzorise belief revision
gen w_revision_change_1yf=revision_change_1yf
centile revision_change_1yf, c(2.5 97.5)
local min=`r(c_1)'
local max=`r(c_2)'
replace w_revision_change_1yf=`min' if revision_change_1yf<`min'
replace w_revision_change_1yf=`max' if revision_change_1yf>`max'


}

*******************************Gen Variables Dependent on Winsorized Vars*****************************
{
	* Difference between desired info and prior 
	gen gap_change_potential = 0.007-prior_change_1yf if qi == 2 
	replace gap_change_potential = 0.065-prior_change_1yf if qi == 1
	label var gap_change_potential "difference between prefered info and prior"

	* Difference between recieved signal and prior 
	gen gap_change_shown = gap_change_potential if info_1==1
	replace gap_change_shown = 0 if info_0==1
	label var gap_change_shown "difference between displayed info and prior, if 1 piece of info shown (0 o/w)"
	
	* High or low prior uncert
	quietly sum uncert_prior_change_1yf, d
	gen high_prior_uncert = uncert_prior_change_1yf> `r(p50)' if !missing(uncert_prior_change_1yf)

}


**** some basic demographic checks *********

gen owner=(own_rent!=2) if own_rent!=.

**Income variable
gen income=.
replace income=5000 if hh_income==1
replace income=15000 if hh_income==2
replace income=25000 if hh_income==3
replace income=35000 if hh_income==4
replace income=45000 if hh_income==5
replace income=55000 if hh_income==6
replace income=67500 if hh_income==7
replace income=87500 if hh_income==8
replace income=125000 if hh_income==9
replace income=175000 if hh_income==10
replace income=250000 if hh_income==11
gen ln_income=log(income)

cap: drop white
gen white=(race==1)
replace gender=. if gender==9
replace hispanic=. if hispanic==9
replace gender=1 if gender==.


gen coll_grad=0
replace coll_grad=1 if education>4
gen HH_income=0
replace HH_income=1 if income>60000
gen female=0
replace female=1 if gender==1

**Owns a residency (=1)
gen own_residency=q4==1

label var HH_income "Income > \\\$60,000 (0/1)"
label var coll_grad "College Graduate (0/1)"
label var age "Age"
label var female "Female (0/1)"
label var married "Married (0/1)"
label var white "White (0/1)"
label var own_residency "Homeowner (0/1)"

sum HH_income coll_grad age female married white own_residency


*******************************Table B.1****************************

{

label var prior_change_1yf  "Prior Belief"
label var post_change_1yf  "Posterior Belief"
replace post_change_1yf = 100 * post_change_1yf
replace prior_change_1yf = 100 * prior_change_1yf

gen confidence_prior=100*uncert_prior_change_1yf
gen confidence_post=100*uncert_post_change_1yf

capt prog drop runmodel //program adapted from main analysis file "Informaiton Acquisition Prepare and Analyze Data.do" - Jun 5th
program runmodel, eclass 
	*Prior*
	foreach var in prior_change_1yf post_change_1yf{
	sum `var'
	gen abs_`var'=abs(`var'-`r(mean)')
	}
	reg prior_change_1yf
	local b_prior=string(_b[_cons], "%9.2f")
	local se_prior=string(_se[_cons], "%9.3f")
	reg abs_prior_change_1yf
	local b_abs_prior=string(_b[_cons], "%9.2f")
	local se_abs_prior=string(_se[_cons], "%9.3f")
	
	sum prior_change_1yf
	local sd_prior=string(`r(sd)',"%9.2f")
	local se_sd_prior=string((`r(sd)'/sqrt(2*(`r(N)'-2))), "%9.3f")
	sum confidence_prior
	local conf_prior=string(`r(mean)',"%9.2f")
	local se_conf_prior=string((`r(sd)'/sqrt(2*(`r(N)'-2))), "%9.3f")
	
	*Post*
	reg post_change_1yf
	local b_posterior=string(_b[_cons], "%9.2f")
	local se_posterior=string(_se[_cons], "%9.3f")
	reg abs_post_change_1yf
	local b_abs_post=string(_b[_cons], "%9.2f")
	local se_abs_post=string(_se[_cons], "%9.3f")
	
	local n1=string(e(N), "%9.0fc")
	sum post_change_1yf
	local sd_posterior=string(`r(sd)',"%9.2f")
	local se_sd_posterior=string((`r(sd)'/sqrt(2*(`r(N)'-2))), "%9.3f")
	sum confidence_post
	local conf_post=string(`r(mean)',"%9.2f")
	local se_conf_post=string((`r(sd)'/sqrt(2*(`r(N)'-2))), "%9.3f")

	file write table "\multicolumn{2}{c}{\textbf{$group}} && \\"_n
	file write table "& Mean &  `b_prior' (`se_prior') & `b_posterior' (`se_posterior')  \\"_n
	file write table "N=`n1' & MAD & `b_abs_prior' (`se_abs_prior') & `b_abs_post' (`se_abs_post')  \\"_n
	file write table " & Uncertainty & `conf_prior' (`se_conf_prior') & `conf_post' (`se_conf_post') \\"_n
	
	// rescaling for convenience
	replace post_change_1yf = 1/100 * post_change_1yf
	replace prior_change_1yf = 1/100 * prior_change_1yf

	keep 	userid p*_change_1yf uncert_p*_change_1yf // the p* stands for prior and post
	renvars userid p*_change_1yf uncert_p*_change_1yf , postfix(_i)
	g aa=1
	save change_i.tmp, replace
	renvars userid* p*_change_1yf* uncert_p*_change_1yf* , subst(_i _j)
	joinby aa  using change_i.tmp // all pairwise matchings within info / noinfo groups
	drop if userid_j==userid_i // without meetings with oneself
	egen double userid_min = rowmin(userid*)
	keep if userid_i == userid_min // and dropping duplicates
	local z = 1.96
	foreach u in prior post{ 
	foreach x in i j {
		gen `u'_min_`x' = `u'_change_1yf_`x' - `z' * uncert_`u'_change_1yf_`x'
		gen `u'_max_`x' = `u'_change_1yf_`x' + `z' * uncert_`u'_change_1yf_`x'
	}

	g `u'_disagree  = ( `u'_change_1yf_i < `u'_change_1yf_j & `u'_max_i < `u'_min_j ) ///
						| ( `u'_change_1yf_i > `u'_change_1yf_j & `u'_min_i > `u'_max_j )
	replace `u'_disagree  = . if `u'_change_1yf_i==. | `u'_change_1yf_j ==.
	replace `u'_disagree = 100*`u'_disagree					

	ivreg2 `u'_disagree , clu(userid_i)
	local b_`u'_d	=string(_b[_cons], "%9.2f")
	local se_`u'_d	=string(_se[_cons], "%9.2f")	

	}

	file write table "& Disagreem. (\%)		 & `b_prior_d' (`se_prior_d')  & `b_post_d' (`se_post_d') \\\addlinespace\midrule" _n
	
end

local cols 7
local caption "Effect of Information-Acquisition on the Distribution of Expectations (Analogue of Table 4 in main text)"
local header "\begin{table}[H]\caption{`caption'} \centering\begin{tabular}{l l c c} \hline\hline"
local footer "\end{tabular}\end{table}"

`fwt' "`header'" _n
`fwt' "& & Prior & Posterior \\" _n
`fwt' "\midrule"_n

preserve
keep if info_2==1
drop if qi==3 //drop if didn't want info
global group "Both Pieces of Info"
runmodel
restore
preserve
keep if info_1==1
drop if qi==3 //drop if didn't want info
global group "One Piece of Info"
runmodel
restore
preserve
keep if info_0==1
drop if qi==3 //drop if didn't want info
global group "Control"
runmodel
restore

`fwt' "`footer'"
}

*******************************Table B.2*****************************

{
local cols 8
local caption "Information Preferences and Beliefs"
local label ""
local header "\begin{table}[H]\caption{`caption'} \centering \begin{tabular}{l*{`cols'}{c}} \toprule"
local footer "\end{tabular}\end{table}"

foreach var in pref1 more_info pref_expert pref_info expert_accu_bi expert_cred_bi {
	quietly su `var'
	local `var'_mean: display %3.2f `r(mean)'
	local N: display %4.0f `r(N)'
}

`fwt' "`header'" _n

`fwt' " & & \multicolumn{4}{c}{Information Preferences} & & \multicolumn{2}{c}{Perceptions} \\ \cline{3-6} \cline{8-9}" _n
`fwt' "\\ " _n
`fwt' "   & \multicolumn{1}{p{1cm}}{\centering Obs} & \multicolumn{1}{p{2cm}}{\centering Prefer Any Info } & \multicolumn{1}{p{2cm}}{\centering Prefer 1yr $|$ Info} & \multicolumn{1}{p{2cm}}{\centering Prefer Both $|$ Info } &  \multicolumn{1}{p{2cm}}{\centering Prefer Expert} & & \multicolumn{1}{p{2cm}}{\centering Find Experts Accurate}  & \multicolumn{1}{p{2cm}}{\centering Trust Experts} \\ " _n
`fwt' "   &  & (1) & (2) & (3) & (4) & & (5)  & (6)  \\ \midrule" _n

`fwt' " All & `N' & `pref_info_mean' & `pref1_mean' & `more_info_mean' & `pref_expert_mean' & &`expert_accu_bi_mean' & `expert_cred_bi_mean'\\ " _n
`fwt' "\\" _n

foreach slice in high_reward education_bi numeracy_bi{
	if "`slice'"== "education_bi" {
		local label_h "College Graduate"
		local label_l "Not College Grad"
		}
	else {
		local label : variable label `slice'
		local label_h "High `label'"
		local label_l "Low `label'"
	}
	foreach var in pref1 more_info pref_expert pref_info expert_accu expert_cred expert_accu_bi expert_cred_bi {
		quietly su `var' if `slice'==1
		local `var'_mean_1: display %3.2f `r(mean)'
		local N_1: display %4.0f `r(N)'
		
		quietly su `var' if `slice'==0
		local `var'_mean_0: display %3.2f `r(mean)'
		local N_0: display %4.0f `r(N)'
		
		quietly ttest `var', by(`slice')
		local `var'_p: display %4.3f `r(p)'
	}
	local `slice'_label:  variable label `slice'
	
	`fwt' " `label_h' & `N_1' & `pref_info_mean_1' & `pref1_mean_1' & `more_info_mean_1' & `pref_expert_mean_1' & &`expert_accu_bi_mean_1' & `expert_cred_bi_mean_1' \\" _n
	`fwt' " `label_l' & `N_0' & `pref_info_mean_0'  & `pref1_mean_0' & `more_info_mean_0' & `pref_expert_mean_0'&  &`expert_accu_bi_mean_0' & `expert_cred_bi_mean_0' \\" _n
	`fwt' " P-value & & `pref_info_p' & `pref1_p' & `more_info_p' & `pref_expert_p' & & `expert_accu_bi_p' & `expert_cred_bi_p' \\" _n
	`fwt' "\\" _n
}
`fwt' "\hline"_n
`fwt' "`footer'" _n
}







`fwt' "\end{document}"
file close table


*******************************Create Figure B.1*****************************

{

* By prior uncertainty
reg w_revision_change_1yf gap_change_shown  gap_change_potential prior_change_1yf  if high_prior_uncert==0 & info_2==0, robust
local b1=string(_b[gap_change_shown], "%9.3f")
local s1=string(_se[gap_change_shown], "%9.3f")
local n1=string(e(N), "%9.0fc")
reg w_revision_change_1yf gap_change_shown gap_change_potential prior_change_1yf  if  high_prior_uncert==1 & info_2==0, robust
local b2=string(_b[gap_change_shown], "%9.3f")
local s2=string(_se[gap_change_shown], "%9.3f")
local n2=string(e(N), "%9.0fc")

reg w_revision_change_1yf gap_change_shown  gap_change_potential prior_change_1yf  if high_prior_uncert==0 & info_2==0
est store est1
reg w_revision_change_1yf gap_change_shown gap_change_potential prior_change_1yf  if  high_prior_uncert==1 & info_2==0
est store est2
quietly suest est1 est2, vce(robust)
test [est1_mean]gap_change_shown = [est2_mean]gap_change_shown
local p: display %4.3f `r(p)'
est clear

binscatter w_revision_change_1yf gap_change_shown if info_2==0, controls(gap_change_potential prior_change_1yf) by(high_prior_uncert) scheme(s1mono)  ///
	ytitle("Posterior Belief - Prior Belief", height (5)) xtitle("Information Value - Prior Belief", height (5)) ///
	note("          Slope{subscript:Below-median uncertainty} =  `b1' (`s1')" "          Slope{subscript:Above-median uncertainty} =  `b2' (`s2')" "          p-value =  `p'" , justification(left) position(4) ring(0) size(medsmall)  ) ///
	legend(label(1 "Below-median uncertainty (N=`n1')") label(2 "Above-median uncertainty (N=`n2')")  region(color(none)) cols(1) position(11) ring(0) size(medsmall))  ///
	xlabel(-0.04 "-4%" -0.03 "-3%" -0.02 "-2%" -0.01 "-1%" 0 "0%" 0.01 "1%" 0.02 "2%" 0.03 "3%" 0.04 "4%" 0.05 "5%" 0.06 "6%") ///
	ylabel( -0.04 "-4%" -0.03 "-3%" -0.02 "-2%" -0.01 "-1%" 0 "0%" 0.01 "1%" 0.02 "2%" 0.03 "3%")
graph export ${output}FigureB1.pdf, replace



}